<?php
class control extends base
{
	function control()
	{
		$this->base();
		getgpc('user_id','S')?'':exit('无权限');		
		$this->load_class('pm');
		$this->load_class('user');
	}
	//发送短信
	function actionpm()
	{
		if(!empty($_POST))
		{
			$_POST = escape($_POST);
			$msgto_name = $_POST['msgto'];
			$msg_subject = $_POST['subject'];
			$msg_content = $_POST['content'];
			$msgfrom_id = $_SESSION['user_id'];
			$msgfrom_name = $_SESSION['user_name'];
			$ctime = time();
			if(empty($msgto_name))
			{
				$this->redirect('index.php?m=pm&a=pm',4000,'收件人不能为空');
				exit();
			}
			if(empty($msg_subject)||empty($msg_content))
			{
				$this->redirect('index.php?m=pm&a=pm',4000,'标题,内容不能为空');
				exit();
			}
			$msgto = $_ENV['user']->check_username($msgto_name);
			if(!empty($msgto))
			{
				require_once AM_ROOT.'levelconfig.inc.php';
				$msgto_id = $msgto[0]['user_id'];
				$row = array(
				'msgfrom'=>"$msgfrom_name",
				'msgfromid'=>"$msgfrom_id",
				'subject'=>"$msg_subject",
				'created'=>"$ctime",
				'message'=>"$msg_content",
				'msgtoid'=>"$msgto_id",
				'msgto'=>"$msgto_name"
				);
				$result = $_ENV['pm']->pm($row);
				if($result)
				{
				$row2 = array('pm_count'=>'pm_count+1');
				$_ENV['user']->update($row2);
				$this->redirect('index.php?m=pm&a=outbox',4000,'发送成功!');
				exit();
				}
				else
				{
					$this->redirect('index.php?m=pm&a=pm',4000,'发送失败,请重试');
					exit();
				}
			}
			else
			{
				$this->redirect('index.php?m=pm&a=pm',4000,'该用户不存在');
				exit();
			}
		}
		$msgto = isset($_GET['username'])?escape($_GET['username']):'';
		$this->view->assign('msgto',$msgto);
		$this->view->display('pm.htm');
	}
	//发件箱
	function actionoutbox()
	{
		$opage = isset($_GET['opage'])?intval($_GET['opage']):1;
		$operpage  = 10;
		$of = ($opage-1)*$operpage;
		$uid = intval($_SESSION['user_id']);
		$onum = $_ENV['pm']->get_count_byfid($uid);
		$outmulti = $this->multi($onum,$operpage,$opage,'index.php?m=pm&a=outbox','opage');
		$outdata = $_ENV['pm']->get_outdata($uid,$of,$operpage);
		$this->view->assign('outmulti',$outmulti);
		$this->view->assign('outdata',$outdata);
		$this->view->display('outbox.htm');
	}
	//收件箱
	function actioninbox()
	{
		$ipage = isset($_GET['ipage'])?intval($_GET['ipage']):1;
		$iperpage  = 10;
		$from = ($ipage-1)*$iperpage;
		$uid = intval($_SESSION['user_id']);
		$inum = $_ENV['pm']->get_count_byiid($uid);
		$inmulti = $this->multi($inum,$iperpage,$ipage,'index.php?m=pm&a=inbox','ipage');
		$indata = $_ENV['pm']->get_indata($uid,$from,$iperpage);
		$this->view->assign('inmulti',$inmulti);
		$this->view->assign('indata',$indata);
		$this->view->display('inbox.htm');
	}
	function actionupdate()
	{
		$pmid =is_numeric(getgpc('pmid','P'))?intval(getgpc('pmid','P')):'0'; 
		$result = $_ENV['pm']->update($pmid);
		echo $result;	
	}
	function actiondelete()
	{
		if(getgpc('type','G')=='inbox')
		{
		$type=2;
		$url='index.php?m=pm&a=inbox';
		}
		elseif(getgpc('type','G')=='inbox')
		{
		$type=1;
		$url='index.php?m=pm&a=outbox';		
		}
		$pmid = is_numeric(getgpc('pmid','G'))?intval(getgpc('pmid','G')):0;
		$owner = getgpc('user_id','S');
		$result = $_ENV['pm']->delete($pmid,$owner,$type);
		if($result)
		{		
			$this->redirect($url,4000,'删除成功!');
			exit();
		}
	}
}
?>